<?php
	require_once('event.class.php');
	
	/**
	 * The database event class helps to manage an event using information
	 * from a MySQL database.
	 */
	class DatabaseEvent extends Event {
		/**
		 * The event's id
		 */
		private $id;
		
		/**
		 * Initializes a new instance of the DatabaseEvent class.
		 * 
		 * @param int $id The event's id
		 * @param DateTime $dateTime Date and time of the event
		 * @param string $title A title for the event
		 * @param string $description A description for the event
		 * @param bool $pending Whether or not the event is approved
		 */
		public function __construct($id, $dateTime, $title, $description = '', $pending = false) {
			parent::__construct($dateTime, $title, $description, $pending);
			$this->setId($id);
		}
		
		/**
		 * Gets the id of the event from the database.
		 * 
		 * @return int the event's id
		 */
		public function getId() {
			return $this->id;
		}

		/**
		 * Sets the event's id
		 * 
		 * @param int $id the event's id
		 * @return void
		 */
		public function setId($id) {
			$this->id = (int)$id;
		}

		/**
		 * Inserts this event into the database.
		 * 
		 * @param mixed $databaseConnection the database connection to use
		 * @return void
		 */
		public function write($databaseConnection) {
			$date = $this->getDateTime()->format('c');
			$title = $this->getTitle();
			$description = $this->getDescription();
			$pending = $this->getPending();
			if (0 === get_magic_quotes_gpc()) {
				$date = addslashes($date);
				$title = addslashes($title);
				$description = addslashes($description);
				$pending = addslashes($pending);
			}
			mysql_query('INSERT INTO events (datetime, title, description, pending) VALUES ("'.$date.'", "'.$title.'", "'.$description.'", "'.$pending.'")', $databaseConnection);
		}
		
		/**
		 * Fills this event using the information from $databaseRecord
		 * 
		 * @param mixed $databaseRecord an associative array containing all information about the event
		 * @retur void
		 */
		public function read($databaseRecord) {
			$this->setDateTime(new DateTime($databaseRecord['datetime']));
			$this->setId($databaseRecord['id']);
			$this->setTitle($databaseRecord['title']);
			$this->setDescription($databaseRecord['description']);
			$this->setPending($databaseRecord['pending']);
		}
		
		/**
		 * Deletes the current event from the database.
		 *
		 * @param mixed $databaseConnection the database connection to use  
		 * @return void
		 */
		public function delete($databaseConnection) {
			$id = $this->getId();
			if (0 === get_magic_quotes_gpc()) $id = addslashes($id);
			mysql_query('DELETE FROM events WHERE id = '.$id, $databaseConnection);
		}

		/**
		 * Marks the current event as approved
		 * 
		 * @param mixed $databaseConnection the database connection to use
		 * @return void
		 */		
		public function approve($databaseConnection) {
			$id = $this->getId();
			if (0 === get_magic_quotes_gpc()) $id = addslashes($id);
			mysql_query('UPDATE events SET pending = 0 WHERE id = ' . $id, $databaseConnection);
			$this->setPending(false);
		}
	}